<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_saveload"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            left: 250px;
            right: 0px;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #menu {
            position: absolute;
            float: left;
            background: #ffffff;
            width: 250px;
            height: 100%;
            border: 1px solid #3473b7;
            z-index: 100;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            width: 315px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
        
        #toolbar .panel-title {
            font-size: 16px;
            color: white;
        }

        .input-group {
            margin-bottom: 10px;
        }

        .panel-title {
            font-size: 16px;
            color: #ffffff;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.title_saveload"></h5></div>
    <div class='panel-body content'>
        <div class='panel'>
            <div class='input-group' style="margin-top:15px;margin-left: 50px;">
                <select class="search-query form-control" id="SLT" style="width: 200px"></select>
            </div>
            <div class='input-group' style="margin-left: 20px;">
                <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_load"
                       onclick="loadSimulationMap()"/>&nbsp;
                <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_cancelMarker"
                       onclick="PlottingDrawCancel()"/>&nbsp;
                <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear"
                       onclick="PlottingClear()"/>&nbsp;
                <input type="button" class="btn btn-default " data-i18n="[value]resources.btn_save" onclick="save()"/>
            </div>
            <div class='input-group' style="margin-left: 50px;margin-bottom: 15px;">
                <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_saveAs"
                       onclick="SaveAsSmlFile()"/>
                <input type="text" id="txt" class='form-control' style="width: 100px;float: right"/>
            </div>
        </div>
    </div>
</div>
<div id="menu">
    <div class="easyui-panel" style="position:absolute;top:0px;bottom:0px;left:0px;right:0px;padding:5px; width: 100%;">
        <div class="easyui-tabs" style="width: 100%;height: 100%">
            <div id="plotPanel" data-i18n="[title]resources.text_drawPanel" style="overflow: hidden"></div>
            <div id="stylePanel" data-i18n="[title]resources.text_attributePanel"></div>
        </div>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" include="iclient8c-plot,PlottingPanel"
        src="../../dist/classic/include-classic.js"></script>
<script>
    var map, plottingLayer, layer, plotting, plotPanel, stylePanel, plottingEdit, drawGraphicObjects = [];
    var select, SMLName = null;
    var host = window.isLocal ? window.server : "http://iclsvr.supermap.io";
    var mapurl = host + "/iserver/services/map-china400/rest/maps/China_4326";
    var serverUrl = host + "/iserver/services/plot-jingyong/rest/plot/";

    init();

    function init() {
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.LayerSwitcher(),
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", mapurl, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
        //总控类
        plotting = SuperMap.Plotting.getInstance(map, serverUrl);
        sitManager = plotting.getSitDataManager();
        sitManager.events.on({
            "getSMLInfosCompleted": getSMLInfosSucess,
            "getSMLInfosFailed": getSMLInfosFail,
            "openSmlFileCompleted": openSuccess,
            "openSmlFileFailed": openFail,
            "saveSmlFileCompleted": saveSuccess,
            "saveSmlFileFailed": saveFail
        });
        plottingLayer = new SuperMap.Layer.PlottingLayer("标绘图层", serverUrl);
        //空间分析服务地址：目前使用的是服务器默认空间分析地址，可更换成实际使用的空间分析服务地址
        //plottingLayer.spatialAnalystUrl = host + "/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";

        plottingLayer.style = {
            fillColor: "#66cccc",
            fillOpacity: 0.4,
            strokeColor: "#66cccc",
            strokeOpacity: 1,
            strokeWidth: 3,
            pointRadius: 6
        };
        //态势标绘编辑
        plottingEdit = new SuperMap.Control.PlottingEdit();


        // 绘制标号;
        var drawGraphicObject = new SuperMap.Control.DrawFeature(plottingLayer, SuperMap.Handler.GraphicObject);
        drawGraphicObjects.push(drawGraphicObject);
        //添加态势标绘控件
        map.addControls([plottingEdit, drawGraphicObject]);
    }

    function addLayer() {
        map.addLayers([layer, plottingLayer]);
        map.setCenter(new SuperMap.LonLat(104, 35), 3);
        //创建标绘面板
        plotPanel = new SuperMap.Plotting.PlotPanel("plotPanel", serverUrl, map);
        plotPanel.events.on({"initializeCompleted": initializeCompleted});
        plotPanel.initializeAsync();
        //创建属性面板
        stylePanel = new SuperMap.Plotting.StylePanel("stylePanel");
        stylePanel.addEditLayer(plottingLayer);

        //获取标号的列表
        getSMLInfos();
    }

    function initializeCompleted() {
        if (drawGraphicObjects.length > 0) {
            plotPanel.setDrawFeature(drawGraphicObjects[0]);
        }
    }

    //取消标绘
    function plottingAllDeactivate() {
        for (var i = 0; i < drawGraphicObjects.length; i++) {
            drawGraphicObjects[i].deactivate();
        }
        plottingEdit.deactivate();
    }

    //取消标绘，激活标绘编辑控件
    function PlottingDrawCancel() {
        widgets.alert.clearAlert();
        plottingAllDeactivate();
        plottingEdit.activate();
    }

    function getSMLInfos() {
        sitManager.getSMLInfos(0, 10);
    }

    function getSMLInfosSucess(result) {
        select = document.getElementById("SLT");
        while (select.hasChildNodes()) {
            select.removeChild(select.firstChild);
        }
        for (var i = 0, len = result.length; i < len; i++) {
            var options = document.createElement("option");
            options.setAttribute("value", result[i].SMLFileName);
            options.innerHTML = result[i].SMLFileName;
            select.appendChild(options);
        }
        return false;
    }

    function getSMLInfosFail(result) {
        console.log(result);
    }

    //态势图保存
    function save() {
        widgets.alert.clearAlert();
        plottingAllDeactivate();
        sitManager.saveSmlFile();
    }

    //态势图另存为
    function SaveAsSmlFile() {
        plottingAllDeactivate();
        SMLName = document.getElementById("txt").value;
        if (SMLName.length !== 0) {
            sitManager.saveAsSmlFile(SMLName);
        } else {
            widgets.alert.showAlert(resources.msg_inputName, true);
        }
    }

    function saveSuccess() {
        getSMLInfos();
    }

    function saveFail() {

    }

    //加载态势图
    function loadSimulationMap() {
        widgets.alert.clearAlert();
        var select = document.getElementById("SLT");
        for (var i = 0; i < select.children.length; i++) {
            if (select.children[i].selected) {
                sitManager.openSmlFileOnServer(select.children[i].value);

            }
        }
    }

    function openSuccess() {
        var sitDataLayers = sitManager.getSitDataLayers();
        drawGraphicObjects = [];
        for (var i = 0; i < sitDataLayers.length; i++) {
            drawGraphicObjects.push(sitDataLayers[i].drawGraphicObject);
            stylePanel.addEditLayer(sitDataLayers[i]);
        }
        plotPanel.setDrawFeature(drawGraphicObjects[0]);
    }

    function openFail() {
        console.log("error");
    }

    //清空绘制
    function PlottingClear() {
        widgets.alert.clearAlert();
        plottingAllDeactivate();
        for (var i = 0; i < map.layers.length; i++) {
            if (map.layers[i].CLASS_NAME === "SuperMap.Layer.PlottingLayer") {
                map.layers[i].removeAllFeatures();
            }
        }
    }

    document.onmouseup = function (evt) {
        var evt = evt || window.event;
        if (evt.button === 2) {
            PlottingDrawCancel();
            return false;
        }
        evt.stopPropagation();
    }

</script>

</body>
</html>